
    <script type="text/javascript">
        portlets=new Array();
        index=0;
        function load_portlet(pname,purl,pcontainer,pid,ptitle)
        {


            <?php blockUI($var_portal_portletloading); ?>
            portlets.push({'name':pname,'url':purl,'container':pcontainer,'id':pid,'title':ptitle});
        }

        
        function setup_portal()
        {

        if (index<portlets.length)
            setup_portlet(portlets[index].name,portlets[index].url,portlets[index].container,portlets[index].id,false,portlets[index].title);
        }
        function setup_portlet(name,url,container,id,isnew,title)
        {
            if (!isnew) index++;
//alert(container+" "+id+" "+title);
            if (container=="") container="container1";

            $.get("/portlet.php", { app: url,language:"<?php echo $lang;?>",mode:"edit",coupleId:id},
                    function(data){
                        $.unblockUI();
                        $("#"+container).portal("portlet_"+id, {

                            title:      title,

                            content:    data
                        });
                        if (!isnew) setup_portal();
                    });
        }

        function move_portlet(id,container,order)
        {
            id=id.replace("portlet_", "");

            $.get("/portlet.php",
                    {app: "system",
                        operation:"move_portlet",
                        container:container,
                        coupleId:id,
                        order:order},
                    function(data){
                        //ssssssssss
                    });
        }

        function remove_portlet(id,object)
        {

            <?php blockUI($var_portal_portletunloading); ?>
            id=id.replace("portlet_", "");

            $.get("/portlet.php",
                    {app: "system",
                        operation:"remove_portlet",
                        coupleId:id},
                    function(data){

                        $.unblockUI();
                        if (data=="ok")  object.parents(".portlet").remove();
                        $(".modal").remove();
                        $.portal.remember();
                    });
        }
        function add_portlet(name,url,container,portletId,title)
        {



            <?php blockUI($var_portal_portletadding); ?>

            $.get("/portlet.php",
                    { app: "system",
                        operation:"add_portlet",
                        portlet:name,
                        container:container,
                        pageId:<?php echo $pageId; ?>,
                        portletId:portletId},
                    function(data){
                        if (IsNumeric(data))  {
                            coupleId=data;

                            reload_preferences(name,url,container,coupleId,true,title);



                        }
                    });


        }

        function reload_preferences(name,url,container,coupleId,isnew,title){

            $.get("/portlet.php",
                    {app: "system",
                        operation:"reload_preferences",
                        coupleId:coupleId},
                    function(data){

                        if (data=="ok") {
                            //alert("Portlet preferences reloaded !\n The page will be reloaded.");
                            setup_portlet(name,url,container,coupleId,isnew,title);

                        }
                        else  alert("There was an error, please contact your administrator !");


                    });
        }



    </script>



    <div  class="portlets" id="portlets_list">
        <div class="header portlet-header ui-state-default">
            <img  src="/images/icons/close.png" onclick="toogle_edit()"   title="Close">
            <span class="title">Portlets List</span></div>
        <div class="systemportlets">

            <ul>
                <?php

                $sSql ="Select portal_portlet.title,
  portal_portlet.appname,
  portal_portlet.appurl,
  portal_portlet.id
From portal_portlet Inner Join
  portal_portal_app On portal_portlet.appId = portal_portal_app.appId
Where portal_portal_app.portalId = ".$portalId;//." OR portal_portlet.portalId IS NULL OR `portalId`='".$_GET["portalId"]."'"";
                $rs=$GLOBALS["conn"]->Execute($sSql);

                while ( !$rs->EOF)  {
                    ?>
                    <li ondblclick="add_portlet('<?php echo $rs->fields["appname"]; ?>','<?php echo $rs->fields["appurl"]; ?>','','<?php echo $rs->fields["id"]; ?>','<?php echo $rs->fields["title"]; ?>')"><?php echo $rs->fields["title"]; ?></li>
                    <?php
                    $rs->MoveNext();
                }
                ?>
            </ul>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            $("#portlets_list").draggable();
        });
    </script>


        <?php




        //echo "<script type="text/javascript">alert('".$layout."')</script>";
        echo $layout;

//Portlets
        $sSql="Select portletpage.orderCmpnt as portletOrder, ".
            "portletpage.id, ".
            "portlet.title, ".
            "portlet.appname, ".
            "portlet.appurl, ".
            "CAST(REPLACE(portletpage.place, 'container', '') AS SIGNED) as place ".
            "From portal_portletpage portletpage Inner Join ".
            "portal_portlet portlet On portlet.id = portletpage.portletId Inner Join ".
            "portal_pages pages On pages.id = portletpage.pageId ".
            "Where pages.portalId = ".$portalId." And pages.name = '".$pageName."' ".
            "Order By place,portletOrder Asc";

//echo  $sSql;
        $rs=$GLOBALS["conn"]->Execute($sSql);
        ?>
    <script type="text/javascript">
            <?php
            while ( !$rs->EOF)  {
                ?>
            load_portlet("<?php echo $rs->fields["appname"];?>","<?php echo $rs->fields["appurl"];?>","container<?php echo $rs->fields["place"];?>","<?php echo $rs->fields["id"];?>","<?php echo $rs->fields["title"];?>");
                <?php

                $rs->MoveNext();
            }


            ?>
  setup_portal();

       
        </script>



